/******************************************************************
NOTES: 
This do file is based on the do file "Trend_figures.do". 
;



*******************************************************************/

# delim ;  
clear all;
set matsize 11000 ;
eststo clear ;
capture program drop add_lab_FE;
capture log close ; 
log using "../logs/create_national_vars.log", text replace;

*settings some settings that are common for the set of plots on this dofile; 

local pdf_plot_settings = 	`"
				xmtick(1980(1)2008)
				xlabel(1980(10)2010)
				ylabel(#3,)
				ymticks(##4)
				scale(*1.5) 
				xtitle("")
				legend(off)"';
*scale(*1.3): This rescales the font size of the whole figure;	
********************************************************************;

	
use "../data/state_year_all_80_15.dta", clear  ;





*---------------------------------------------------;
* - Generate variables and general cleanings ;
*---------------------------------------------------;
do "_setup_expenditure.do";
do "_cleaning_and_new_variables.do";
xtset state year;
gen Dmiles=d.u_miles_IH ;

gen no_exp=cond(exp_L_IH_SF12a_r==. | exp_L_IH_SF12a_r==. | exp_L_IH_SF12a_r<0,1,0);


*Defining macro variables; 
global lm_var u_lane_miles_IH u_lane_miles_rural u_lane_miles_urban u_miles_IH
				 s_lane_miles_IH s_lane_miles_rural_IH s_lane_miles_urban_IH s_lane_miles_rig_IH
				 ;

				 
gen lm_IH_h2O= 		u_lane_miles_IH*sh_h20_10m_s; 
gen lm_IH_urb= 		u_lane_miles_IH*sh_urb_10m_s; 
gen lm_IH_elev= 	u_lane_miles_IH*sh_elev_10m_s; 
gen lm_IH_sd_elev=	u_lane_miles_IH*sh_sd_elev_10m_s ;
gen lm_IH_temp= 	u_lane_miles_IH*sh_temp_10m_s; 
gen lm_IH_precip= 	u_lane_miles_IH*sh_precip_10m_s; 
gen lm_IH_sh_new_lm  = 	u_lane_miles_IH*sh_new_lm;
gen lm_IH_unionization= 	u_lane_miles_IH*unionization;
gen lm_IHunionization_Mem_Priv_Cons= 	u_lane_miles_IH*unionization_Cov_Priv_Cons ;
gen lm_IHunionization_Mem_Total= 	u_lane_miles_IH*unionization_Cov_Total ;


gen lm_IH_grade = u_lane_miles_IH*s_average_grade_seg_IH ;
format lm_IH_grade  %3.2fc;
gen lm_IH_new=Du_lane_miles_IH*sh_new_lm;
*replace lm_IH_new=. if year==1984;

global comp_var lm_IH_h2O    lm_IH_urb    lm_IH_elev    lm_IH_temp    lm_IH_precip lm_IH_sd_elev lm_IH_grade lm_IH_new  Dmiles
				sh_h20_10m_s sh_urb_10m_s sh_elev_10m_s sh_temp_10m_s sh_precip_10m_s sh_sd_elev_10m_s 
				lm_IH_unionization  lm_IHunionization_Mem_Priv_Cons  lm_IHunionization_Mem_Total;

gen u_inches_IH=	u_iri_IH*u_lane_miles_IH;
gen s_inches_IH=	s_iri_IH*s_lane_miles_IH;
drop u_iri_IH s_iri_IH s_trucks_av_IH;



global exp_var   exp_L_IH_SF12a_r exp_IRI_IH_SF12a_r exp_IH_mtn_SF12a_r exp_IH_mtn_SF12_r
				 exp_relocation_IH_all_r 
				s_vmt_IH u_vmt_IH s_vmt_trucks_av_IH s_vmt_trucks_peak_IH  u_inches_IH s_inches_IH
				s_vmt_NHS ;	

global rev_var rev_grand_total_mt_r rev_total_h_r rev_total_mt_r rev_total_m_fuel_h_r;
				
*---------------------------------------------------;
* - Collapse (year) ;
*---------------------------------------------------;

*u_lane_miles_IH
*conversion factor to get to 10^6 2010 dollars;
		
sort year;
collapse (mean) DGS10_r asphalt_price Surf_BituminousconcreteAv_ctn10

		 (sum) ${lm_var}
			    ${exp_var}
			   ${rev_var}
		     , by(year);


gen u_aadt_IH=		(u_vmt_IH/u_lane_miles_IH)/365;
gen s_aadt_IH=		(s_vmt_IH/s_lane_miles_IH)/365;
gen s_aadt_trucks_av_IH=	s_vmt_trucks_av_IH /s_lane_miles_IH;
gen s_aadt_trucks_peak_IH=	s_vmt_trucks_peak_IH/s_lane_miles_IH;
gen s_iri_IHnat = 	s_inches_IH/s_lane_miles_IH;
gen u_iri_IHnat = 	u_inches_IH/u_lane_miles_IH;



label var exp_L_IH_SF12a_r		"Construction USD2010";
label var exp_IRI_IH_SF12a_r 	"Resurfacing USD2010";
label var exp_IH_mtn_SF12a_r 	"Maintenance USD2010";
label var u_lane_miles_IH 		"Lane Miles IH, HPMS Universe";
label var s_lane_miles_IH 		"Lane Miles IH, HPMS Sample";	
label var u_miles_IH 		"Miles IH, HPMS Universe";
label var u_aadt_IH				"AADT IH, HPMS Universe";	
label var s_aadt_IH				"AADT IH, HPMS Sample";	
label var s_iri_IHnat			"IRI IH, HPMS Sample";
label var u_iri_IHnat			"IRI IH, HPMS Universe";  
label var u_vmt_IH "VMT IH, Universe";
label var s_aadt_trucks_av_IH "Trucks";
label var  s_aadt_trucks_peak_IH  "Trucks peak hours";
label var rev_total_mt_r "Total revenue highway and mass transit account USD2010 [source: FE9 p2 ]";
label var rev_total_m_fuel_h_r   "Total Revenue motor fuel (Highway motor fuel)  USD2010 [source: FE9 p1 ]";
label var rev_total_h_r  "Total revenue  USD2010 [source: FE9 p1]" ;
label var  rev_grand_total_mt_r "Total revenue mass transit account USD2010 [source: FE9 p2 ]" ;
label var asphalt_price "Asphault prices";

label var Surf_BituminousconcreteAv_ctn10 "Asphault Prices";
drop asphalt_price u_lane_miles_rural u_lane_miles_urban s_lane_miles_rural_IH s_lane_miles_urban_IH s_lane_miles_rig_IH exp_IH_mtn_SF12_r exp_relocation_IH_all_r  u_inches_IH s_inches_IH ;



*ADD COEFFECIENTS ;

merge 1:1 year  using "../intermediate_data/construction_betas_1.dta", nogen ;
ren beta P_const_beta;
ren se P_const_se;
label var P_const_beta "Coefficients Construction";
label var P_const_se "SE Construction";
drop dummy up down;
merge 1:1 year  using "../intermediate_data/maintF12a_betas_1.dta", nogen ;
ren beta P_maint_beta;
ren se P_maint_se;
label var P_maint_beta "Coefficient Maintenance";
label var P_maint_se "SE Maintenance";
drop dummy up down;
merge 1:1 year  using "../intermediate_data/resurf_betas_1.dta", nogen ;
ren beta P_resurf_beta;
ren se P_resurf_se;
label var P_resurf_beta "Coefficient Resurfacing" ;
label var P_resurf_se "SE Resurfacing" ;
drop dummy up down;

replace s_iri_IHnat=. if year<1990;
replace u_iri_IHnat=. if year<1993;
foreach var in exp_L_IH_SF12a_r exp_IRI_IH_SF12a_r exp_IH_mtn_SF12a_r{;
	replace `var'=. if year <1984;
};



foreach var in rev_grand_total_mt_r rev_total_h_r rev_total_mt_r rev_total_m_fuel_h_r s_lane_miles_IH{;
	replace `var'=. if `var'==0;
};




**Save;
save "../intermediate_data/national_vars" , replace ;

log close;
exit;
